Convert 1D Array Into 2D Array
Question
Given a one-dimensional array of integers, write a function to convert it into a two-dimensional array of the same size.
Example 1
Input: [1, 2, 3, 4, 5]
Output: [[1, 2], [3, 4], [5]]
Solution
- ▭
- ▯
all//Convert 1D Array Into 2D Array.py
def convert_1d_to_2d(arr):
result = []
row_size = 2
for i in range(0, len(arr), row_size):
result.append(arr[i:i+row_size])
return result
arr = [1,2,3,4,5,6]
print(convert_1d_to_2d(arr))
# [[1, 2], [3, 4], [5, 6]]
all//Convert 1D Array Into 2D Array.py
def convert_1d_to_2d(arr):
result = []
row_size = 2
for i in range(0, len(arr), row_size):
result.append(arr[i:i+row_size])
return result
arr = [1,2,3,4,5,6]
print(convert_1d_to_2d(arr))
# [[1, 2], [3, 4], [5, 6]]